package com.google.code.wicket.auth.component.value.provider.session;

import org.apache.wicket.Session;

import com.google.code.wicket.auth.component.value.provider.SingleValueProvider;

/**
 * A Provider for authentication and authorization based on a custom session
 * implementation. Override this and provide implementations for
 * {@link #isLoggedIn(Session)} and {@link #hasPrivilege(Session, Object)}.
 * 
 * @param <S>
 *            Session type
 * @param <T>
 *            Privilege type
 */
public abstract class SessionBasedSingleValueProvider<S extends Session, T>
        extends SingleValueProvider<T> implements
        SessionSingleAuthorizationProvider<S, T> {

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    @Override
    public final boolean hasValue(final T value) {

        boolean result;
        if (value == null) {
            result = true;
        } else {
            final S session = (S) Session.get();
            result = this.isLoggedIn(session)
                    && this.hasPrivilege(session, value);
        }
        return result;
    }

}
